# 获取CSV数据
import csv
import os

file_path = "./src/data"
def get_csv_data(file_path):
    """获取CSV数据

    Args:
        file_path (str): CSV文件路径

    Returns:
        Tuple[Dict, Dict]: 包含两个字典的元组，第一个字典为名称到值的映射，第二个字典为值到名称的映射
    """
    raw_data = {}
    oid_data = {}
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            reader = csv.reader(file)
            for raw in reader:
                if len(raw) < 3:
                    continue  # 跳过列数不足的行
                if raw[0] == "Name":
                    continue
                raw_data[raw[0]] = raw[2]
                oid_data[raw[2]] = raw[0]
    except FileNotFoundError:
        print(f"文件未找到: {file_path}")
        return {}, {}
    except Exception as e:
        print(f"读取文件时发生错误: {e}")
        return {}, {}
    
    return raw_data, oid_data

def get_all_csv_data():
    """获取所有CSV数据

    Args:
        file_path (str): CSV文件所在的目录路径

    Returns:
        Dict: 所有CSV数据列表
    """
    data = {}
    try:
        for file in os.listdir(file_path):
            if file.endswith(".csv"):
                try:
                    raw_data, oid_data = get_csv_data(os.path.join(file_path, file))
                    file_name = os.path.splitext(file)[0]
                    data[file_name] = {"raw_data": raw_data, "oid_data": oid_data}
                except Exception as e:
                    print(f"Error reading file {file}: {e}")
        if not data:
            print("No CSV files found in the specified directory.")
    except Exception as e:
        print(f"Error accessing directory {file_path}: {e}")
    return data

def get_oid_data():
    """获取所有OID数据

    Returns:
        Dict: 所有OID数据列表
    """
    data = {}
    
    # 验证文件路径是否存在且为目录
    if not os.path.isdir(file_path):
        raise ValueError(f"Invalid directory path: {file_path}")
    
    try:
        for file in os.listdir(file_path):
            if file.endswith(".csv"):
                try:
                    raw_data, oid_data = get_csv_data(os.path.join(file_path, file))
                    # 使用 os.path.splitext 来获取文件名（不带扩展名）
                    file_name = os.path.splitext(file)[0]
                    data[file_name] = oid_data
                except Exception as e:
                    print(f"Error processing file {file}: {e}")
                    continue
    except Exception as e:
        print(f"Error listing files in directory {file_path}: {e}")
        return {}
    
    return data

def get_raw_data():
    """获取所有raw数据

    Returns:
        Dict: 所有raw数据列表
    """
    data = {}
    try:
        for file in os.listdir(file_path):
            if file.endswith(".csv"):
                raw_data,oid_data = get_csv_data(os.path.join(file_path, file))
                file_name = os.path.splitext(file)[0]
                data[file_name] = raw_data
    except (FileNotFoundError, PermissionError) as e:
        print(f"Error accessing directory {file_path}: {e}")
        return {}
    return data


if __name__ == "__main__":
    data = get_raw_data()
    print(data)